package baiduClub.schedule;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class EditActivity extends Activity
{
	
	
	private static final String[] TURNS_STRINGS = {"第一大节","第二大节","第三大节","第四大节","第五大节","第六大节"};
	private Animation spinnerAnimation;
	
	private int turnID;
	private int week;
	private int dayOfWeek;
	
	private String placeString;
	private String courseNameString;
	private EditText courseNameEditText;
	private EditText placeEditText;
	private Button confirmButton;
	private Button cancelButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		//无标题栏
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.edit_activity_layout);
		
//		turnsSpinner = (Spinner)findViewById(R.editID.spinner);
//		
//		spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, TURNS_STRINGS);
//		spinnerAdapter.setDropDownViewResource(R.layout.spinner_dropdown);
//		turnsSpinner.setAdapter(spinnerAdapter);
//		turnsSpinner.setOnItemSelectedListener(new SpinnerListener());
//		
//		spinnerAnimation = AnimationUtils.loadAnimation(this, R.anim.spinner_animation);
//		turnsSpinner.setOnTouchListener(new SpinnerOnTouchListener());
//		
//		Intent intent = getIntent();
//		turnID = intent.getIntExtra("turn", 0);
//		courseNameString = intent.getStringExtra("courseName");
//		placeString = intent.getStringExtra("place");
//		
//		courseNameEditText = (EditText)findViewById(R.editID.courseNameEditText);
//		courseNameEditText.setText(courseNameString);
//		
//		placeEditText = (EditText)findViewById(R.editID.placeEditText);
//		placeEditText.setText(placeString);
//		
//		confirmButton = (Button)findViewById(R.editID.confirmButton);
//		confirmButton.setOnClickListener(new ConfirmButtonListener());
//		
//		cancelButton = (Button)findViewById(R.editID.cancelButton);
//		cancelButton.setOnClickListener(new CancelButtonListener());
	}
	
	
	class CancelButtonListener implements OnClickListener
	{
		public void onClick(View v) {
			// TODO Auto-generated method stub
		finish();
		}
	}
	
	class ConfirmButtonListener implements OnClickListener
	{
		public void onClick(View v)  {
			// TODO Auto-generated method stub
			Element root = XmlConductor.addFileDocument.getDocumentElement();
			NodeList itemList = root.getElementsByTagName("item");
			Element nodeElement = null;
			for (int i = 0; i < itemList.getLength(); i++) 
			{
				NamedNodeMap attrMap = itemList.item(i).getAttributes();
				String idStr = attrMap.getNamedItem("turn").getTextContent();
				String fromWeekStr = attrMap.getNamedItem("fromWeek").getTextContent();
				String toWeekStr = attrMap.getNamedItem("toWeek").getTextContent();
				String dayOfWeekAttrString = attrMap.getNamedItem("dayOfWeek").getTextContent();
				if (Integer.parseInt(idStr) == turnID && Integer.parseInt(dayOfWeekAttrString) == dayOfWeek) 
				{
					if (Integer.parseInt(fromWeekStr) <= week && week <= Integer.parseInt(toWeekStr)) 
					{
						nodeElement = (Element) itemList.item(i);
						break;
					}
				}
			}
			
			if(nodeElement == null)
			{
				
				nodeElement = XmlConductor.addFileDocument.createElement("item");
				root.appendChild(nodeElement);
			}
			
			//设置节点值
			nodeElement.setAttribute("turn", turnID+"");
			nodeElement.setAttribute("dayOfWeek", dayOfWeek+"");
			nodeElement.setTextContent(courseNameEditText.getText().toString());
			nodeElement.setAttribute("place", placeEditText.getText().toString());
			nodeElement.setAttribute("fromWeek", week+"");
			nodeElement.setAttribute("toWeek", week+"");
			
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			try {
				
				Transformer transformer = transformerFactory.newTransformer();
				transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
				transformer.setOutputProperty(OutputKeys.INDENT, "yes");
				DOMSource domSource = new DOMSource(XmlConductor.addFileDocument);
				
				try 
					{
						PrintWriter printWriter = new PrintWriter(new File("//mnt//sdcard//fubiao.xml"));
						StreamResult streamResult = new StreamResult(printWriter);
						transformer.transform(domSource, streamResult);
					} catch (FileNotFoundException e) 
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
						} 
					catch (TransformerException e) 
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				
			    } catch (TransformerConfigurationException e) 
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			
			finish();
		}
	}
}
